package com.sywb.chuangyebao.view.fragment;

import android.os.Bundle;
import android.view.View;

import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.utils.Utils;
import com.umeng.analytics.MobclickAgent;

import org.bining.footstone.presenter.BasePresenter;
import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.view.BaseFragment;

import java.io.Serializable;

public abstract class BaseStatisticsFragment<T extends BasePresenter> extends BaseFragment<T> {

    private long wpid;
    private long stime = 0L;

    private boolean thisIsVisibleToUser = false;

    protected static Bundle getBundle(Object... pramars) {
        Bundle bundle = new Bundle();
        if (pramars != null && pramars.length > 0) {
            for (int i = 0; i < pramars.length; i++) {
                bundle.putSerializable("p" + i, (Serializable) pramars[i]);
            }
        }
        return bundle;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        if (useRxBus()) {
            RxBus.get().register(this);
        }

        wpid = System.currentTimeMillis() * 100 + Utils.getNum(10, 100);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        thisIsVisibleToUser = isVisibleToUser;

        if (!isVisibleToUser && stime > 0) {
            getStatisticsTime(stime);
            stime = 0L;
        } else {
            if (stime == 0) {
                stime = System.currentTimeMillis();
                getStatisticsPath(getStatisticsIdentity());
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isStatisticsPage()) {
            MobclickAgent.onPageStart(getStatisticsIdentity()); //手动统计页面

            if (thisIsVisibleToUser && stime == 0) {
                stime = System.currentTimeMillis();
                getStatisticsPath(getStatisticsIdentity());
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (isStatisticsPage()) {
            MobclickAgent.onPageEnd(getStatisticsIdentity()); //手动统计页面，必须保证 onPageEnd 在 onPause 之前调用，因为SDK会在 onPause 中保存onPageEnd统计到的页面数据。
            if (stime > 0) {
                getStatisticsTime(stime);
                stime = 0L;
            }
        }
    }

    @Override
    public void onDestroy() {
        if (useRxBus()) {
            RxBus.get().unregister(this);
        }
        super.onDestroy();
    }

    public String getStatisticsIdentity() {
        return this.getClass().getName();
    }

    public boolean isStatisticsPage() {
        return true;
    }

    public int getPageNum() {
        return 0;
    }

    public int getProjectId() {
        return 0;
    }

    public int getNewsId() {
        return 0;
    }

    public void getStatisticsPath(String pageName) {
        HttpUtils.getStatisticsPath(wpid, pageName, getPageNum(), getProjectId(), getNewsId(), new HttpCallback<String>() {
            @Override
            public void onSuccess(String s) {
            }
        });
    }

    public void getStatisticsTime(long startTime) {
        HttpUtils.getStatisticsTime(wpid, startTime, System.currentTimeMillis(), new HttpCallback<String>() {
            @Override
            public void onSuccess(String s) {
            }
        });
    }

}
